package com.cdut.hwadeefsc.controller;

import com.cdut.hwadeefsc.entity.awards;
import com.cdut.hwadeefsc.service.awardsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/api/students")
public class awardsController {

    @Autowired
    private awardsService awardsService;

    // 查看学生奖罚记录
    @GetMapping("/{studentId}/awards")
    public ResponseEntity<List<awards>> getStudentAwards(@PathVariable Integer studentId) {
        try {
            List<awards> awards = awardsService.getStudentAwards(studentId);
            return ResponseEntity.ok(awards);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    // 发布奖罚信息（老师权限）
    @PostMapping("/{studentId}/awards")
    public ResponseEntity<?> addAward(@PathVariable Long studentId, @RequestBody awards award) {
        try {
            award.setStudentId(studentId);
            award.setIssuedAt(LocalDate.now());
            award.setCreatedAt(LocalDate.now());
            boolean success = awardsService.addAward(award);
            return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}
